package com.zjcode.cdcer.service.impl;

import com.chinatechstar.component.commons.entity.MPage;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.service.BaseService;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.SnowflakeIdUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zjcode.cdcer.mapper.CerSysteminitImportInfoMapper;
import com.zjcode.cdcer.mapper.PlanFollowUpMapper;
import com.zjcode.cdcer.mapper.TkmSysOrgMapper;
import com.zjcode.cdcer.mapper.TkmSysUserMapper;
import com.zjcode.cdcer.model.TkmSysOrg;
import com.zjcode.cdcer.model.TkmSysUser;
import com.zjcode.cdcer.model.ZjcodeFollowUpList;
import com.zjcode.cdcer.model.ZjcodePlanFollowUp;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.pojo.CerSysteminitImportInfo;
import com.zjcode.cdcer.service.FollowUpListService;
import com.zjcode.cdcer.service.PlanFollowUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZQM
 * @date 2023/9/21 16:01
 */
@Service
public class PlanFollowUpServiceImpl implements PlanFollowUpService, BaseService {

    @Autowired
    private PlanFollowUpMapper followUpMapper;

    @Autowired
    private CerSysteminitImportInfoMapper cerSysteminitImportInfoMapper;

    @Autowired
    private TkmSysOrgMapper orgMapper;

    @Autowired
    private TkmSysUserMapper userMapper;

    @Autowired
    private FollowUpListService followUpListService;

    @Override
    public JsonObj add(ZjcodePlanFollowUp followUp) {
        followUp.setId(get32UUID());
        Integer i = followUpMapper.add(followUp);
        if (i > 0) {
            // 随访列表添加数据
            followUpListService.add(new ZjcodeFollowUpList(){{
                setPlanFollowUpId(followUp.getId());
                setCerSysteminitImportInfoId(followUp.getCerSysteminitImportInfoId());
            }});
            return JsonObj.success("添加成功");
        } else {
            return JsonObj.error("添加失败");
        }
    }

    @Override
    public JsonObj edit(ZjcodePlanFollowUp followUp) {
        Integer i = followUpMapper.edit(followUp);
        if (i > 0) {
            return JsonObj.success("修改成功");
        } else {
            return JsonObj.error("修改失败");
        }
    }

    @Override
    public JsonObj selById(String id) {
        Map<String, Object> map = new HashMap<>();
        // 查询计划随访信息
        ZjcodePlanFollowUp followUp = followUpMapper.selById(id);
        if (followUp != null) {
            CerSysteminitImportInfo cerSysteminitImportInfo = cerSysteminitImportInfoMapper.selInfoById(followUp.getCerSysteminitImportInfoId());
            map.put("id", followUp.getId());
            map.put("provinceId", followUp.getProvinceId());
            map.put("provinceName", followUp.getProvinceName());
            map.put("cityId", followUp.getCityId());
            map.put("cityName", followUp.getCityName());
            map.put("districtId", followUp.getDistrictId());
            map.put("districtName", followUp.getDistrictName());
            map.put("townshipId", followUp.getTownshipId());
            map.put("townshipName", followUp.getTownshipName());
            map.put("villageId", followUp.getVillageId());
            map.put("villageName", followUp.getVillageName());
            map.put("name", cerSysteminitImportInfo.getName());
            map.put("sfz", cerSysteminitImportInfo.getSfz());
            map.put("phone", cerSysteminitImportInfo.getPhone());
            map.put("address", followUp.getAddress());
            map.put("planTime", followUp.getPlanTime());
            map.put("status", followUp.getStatus());
        }
        return JsonObj.success(map);
    }

    @Override
    public JsonObj del(String id) {
        Integer i = followUpMapper.del(id);
        if (i > 0) {
            return JsonObj.success("删除成功");
        } else {
            return JsonObj.error("删除失败");
        }
    }

    @Override
    public JsonObj changeStatus(String id, Integer status) {
        Integer i = followUpMapper.changeStatus(id, status);
        if (i > 0) {
            if (i == 1) {
                return JsonObj.success("已随访");
            }
            if (i == 2) {
                return JsonObj.success("未随访");
            }
            return JsonObj.success("更改成功");
        } else {
            return JsonObj.error("服务异常");
        }
    }

    @Override
    public Map<String, Object> selList(PageData pageData) {
        // 获取当前用户信息
        Long userId = Long.valueOf(pageData.get("user").toString());
        TkmSysUser userInfo = userMapper.selectByPrimaryKey(userId);
        TkmSysOrg orgInfo = orgMapper.selectByPrimaryKey(userInfo.getOrgId());
        pageData.put("ancestors", orgInfo.getAncestors().replace(",","|"));
        // 解析分页参数
        MPage mPage = getPageInfo(pageData);
        Page<Object> page = PageHelper.startPage(mPage.getCurrentPage(), mPage.getPageSize());
        List<LinkedHashMap<String, Object>> list = followUpMapper.selList(pageData);
        return PaginationBuilder.buildResult(list,
                page.getTotal(),
                mPage.getCurrentPage(),
                mPage.getPageSize());
    }
}
